package blog.com.mw.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import blog.com.mw.base.ResultData;
import blog.com.mw.dao.CategoryDao;
import blog.com.mw.pojo.po.CategoryPO;
import blog.com.mw.pojo.po.CategoryTypePO;
import blog.com.mw.pojo.vo.CategoryTypeVO;
import blog.com.mw.pojo.vo.CategoryVO;
import blog.com.mw.service.CategoryService;

@Service
public class CategoryServiceImpl implements CategoryService {
	
	@Autowired
	private CategoryDao dao;

	@Override
	public ResultData getAllCategoryType(String pageNum,String pageSize,String keyword) {
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			if (pageNum==null || pageSize==null) PageHelper.startPage(1, 10);
			else PageHelper.startPage(Integer.valueOf(pageNum), Integer.valueOf(pageSize));
			List<CategoryTypePO> pos = dao.getAllCategoryType(keyword);
			if(pos!=null) {
				List<CategoryTypeVO> vos = new ArrayList<>();
				pos.forEach(item->{
					CategoryTypeVO vo = new CategoryTypeVO();
					vo.poToVo(item);
					vos.add(vo);
				});
				PageInfo<CategoryTypeVO> pageInfo = new PageInfo<CategoryTypeVO>(vos);
				pageInfo.setTotal(dao.getAllCategoryType(keyword).size());
				data = new HashMap<>();
				data.put("data", pageInfo);
				rs = ResultData.getOk();
				rs.setMsg(pos.size()>0?"获取成功！":"无数据！");
				rs.setData(data);
			}else {
				rs = ResultData.getFail();
				rs.setMsg("获取失败！");
				rs.setData(data);
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;	
	}
	
	@Override
	public ResultData getCategoryById(int id,Boolean isArticleId) {
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			CategoryPO po = isArticleId?dao.getCategoryByArticleId(Long.valueOf(id)):
				dao.getCategoryById(Long.valueOf(id));
			if(po != null) {
				data = new HashMap<>();
				data.put("data", po);
				rs = ResultData.getOk();
				rs.setMsg("获取成功");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("分类不存在！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}


	@Override
	public ResultData addCategory(CategoryVO vo) {
		ResultData rs = null;
		try {
			if(ObjectUtils.isEmpty(vo)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			CategoryPO po = new CategoryPO();
			vo.voToPo(po);
			if(dao.addCategory(po)>0) {
				rs = ResultData.getOk();
				rs.setMsg("添加成功！");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("添加失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}

	@Override
	public ResultData delCategory(int id) {
		ResultData rs = null;
		try {
			if(dao.delCategory(Long.valueOf(id))>0) {
				rs = ResultData.getOk();
				rs.setMsg("删除成功！");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("删除失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}


	@Override
	public ResultData getAllCategory() {
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			List<CategoryPO> pos = dao.getAllCategory();
			if(pos!=null) {
				data = new HashMap<>();
				data.put("data", pos);
				rs = ResultData.getOk();
				rs.setMsg(pos.size()>0?"获取成功！":"无数据！");
				rs.setData(data);
			}else {
				rs = ResultData.getFail();
				rs.setMsg("获取失败！");
				rs.setData(data);
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;	
	}

	
	@Override
	public ResultData getCategoryTypeById(int id) {
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			CategoryTypePO po = dao.getCategoryTypeById(Long.valueOf(id));
			if(po != null) {
				data = new HashMap<>();
				data.put("data", po);
				rs = ResultData.getOk();
				rs.setMsg("获取成功");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("分类不存在！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}


	@Override
	public ResultData addCategoryType(CategoryTypeVO vo) {
		ResultData rs = null;
		try {
			if(ObjectUtils.isEmpty(vo)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			CategoryTypePO po = new CategoryTypePO();
			vo.voToPo(po);
			if(dao.addCategoryType(po)>0) {
				rs = ResultData.getOk();
				rs.setMsg("添加成功！");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("添加失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}

	@Override
	public ResultData delCategoryType(List<Integer> ids) {
		ResultData rs = null;
		try {
			if(ids.size()==0) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			int delSize = dao.delCategoryType(ids);
			if(delSize>0) {
				rs = ResultData.getOk();
				rs.setMsg(delSize==ids.size()?"删除成功！":"部分数据未删除成功！");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("删除失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}
	@Override
	public ResultData getCategoryTypeByContent(String content) {
		ResultData rs = null;
		Map<String,Object> data = null;
		try {
			if(StringUtils.isEmpty(content)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			CategoryTypePO po = dao.getCategoryTypeByContent(content);
			if(po != null) {
				data = new HashMap<>();
				data.put("data", po);
				rs = ResultData.getOk();
				rs.setData(data);
				rs.setMsg("获取成功");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("获取失败");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}




	@Override
	public ResultData updateCategoryType(CategoryTypeVO vo) {
		ResultData rs = null;
		try {
			if(ObjectUtils.isEmpty(vo)) {
				rs = ResultData.getFail();
				rs.setMsg("参数不得为空！");
				return rs;
			}
			CategoryTypePO po = new CategoryTypePO();
			po.setId(Long.valueOf(vo.getId()));
			po.setContent(vo.getContent());
			if(dao.updateCategoryType(po)>0) {
				rs = ResultData.getOk();
				rs.setMsg("修改成功！");
			}else {
				rs = ResultData.getFail();
				rs.setMsg("修改失败！");
			}
		}catch(Exception e) {
			e.printStackTrace();
			rs = ResultData.getFail();
			rs.setMsg("未知异常");
		}
		return rs;
	}
}
